Skip to content

fix accuracy of logit #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Conversation

nsajko
Copy link
Contributor

@nsajko nsajko commented Aug 1, 2025

Fixes #98

@nsajko
Copy link
Contributor Author

nsajko commented Aug 1, 2025

Plot of the error in ULPs after this change:

after

The plotting happens after downsampling (which just takes the maximum value), which itself happens after smoothing (which also just takes the maximum, i.e., a sliding window maximum). Thus the downsampling and smoothing basically just remove the downward spikes of the noise, preserving the maximum error at the region around each point.

Copy link
Collaborator

@tpapp tpapp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for the very clean code!

nsajko added 2 commits August 6, 2025 16:03
oscardssmith has been reviewing the `ULPError` code on
JuliaLang/julia#59087

So sync the code with the changes there.
@tpapp tpapp requested a review from devmotion August 8, 2025 06:50
Comment on lines 7 to 25
# handle floating-point edge cases
if !(isfinite(accurate) && isfinite(approximate))
accur_is_nan = isnan(accurate)
approx_is_nan = isnan(approximate)
if accur_is_nan || approx_is_nan
return if accur_is_nan === approx_is_nan
zero_return
else
inf_return
end
end
if isinf(approximate)
return if isinf(accurate) && (signbit(accurate) == signbit(approximate))
zero_return
else
inf_return
end
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The case isfinite(approximate) and isinf(accurate) is not handled here. Generally, I think it would be clearer to just have a single flat chain of if/elseif statements to handle all cases, e.g.,

if isinan(accurate) || isnan(approximate)
    return accur_is_nan === approx_is_nan ? zero_return : inf_return
elseif isinf(approximate)
    return isinf(accurate) && (signbit(accurate) == signbit(approximate)) ? zero_return : inf_return
else
...
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The case isfinite(approximate) and isinf(accurate) is not handled here.

Indeed. Only the cases not handled correctly by the general formula below are handled specially here.

Generally, I think it would be clearer to just have a single flat chain of if/elseif statements to handle all cases

That's basically what the current situation is, except that the branches are wrapped into the top-level single branch (if !(isfinite(accurate) && isfinite(approximate))) to prevent adverse effect on the performance of the general case.

@nsajko
Copy link
Contributor Author

nsajko commented Aug 8, 2025

Regarding reviewing ULPError, for clarity I'll cross-reference this PR where I'm adding the same module to the base Julia test suite:

It makes sense to keep the changes somewhat synchronized between the two PRs, thus the "update ULPError" commits.

nsajko added a commit to nsajko/julia that referenced this pull request Aug 9, 2025
As suggested by devmotion on a PR to LogExpFunctions.jl:

* JuliaStats/LogExpFunctions.jl#99
nsajko added a commit to nsajko/julia that referenced this pull request Aug 9, 2025
As suggested by devmotion on a PR to LogExpFunctions.jl:

* JuliaStats/LogExpFunctions.jl#99
nsajko added a commit to nsajko/LogExpFunctions.jl that referenced this pull request Aug 9, 2025
As suggested by devmotion on PR JuliaStats#99.
nsajko added a commit to nsajko/LogExpFunctions.jl that referenced this pull request Aug 9, 2025
@nsajko
Copy link
Contributor Author

nsajko commented Aug 9, 2025

In the PR to JuliaLang/julia, I also included some meta-tests to test ulp_error:

Do you want those here, too?

nsajko added a commit to nsajko/julia that referenced this pull request Aug 11, 2025
As suggested by devmotion on a PR to LogExpFunctions.jl:

* JuliaStats/LogExpFunctions.jl#99
nsajko added a commit to nsajko/julia that referenced this pull request Aug 11, 2025
As suggested by devmotion on a PR to LogExpFunctions.jl:

* JuliaStats/LogExpFunctions.jl#99
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

logit(x::Float64) accuracy vanishes around the zero, at x = 1/2
3 participants